package mapgenerator;

import com.utils.MapFileFilter;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/*
 * Project.java
 *
 * Digunakan untuk melakukan pengaturan project
 *
 * @author Sajarwo Anggai
 */
public class Project implements ActionListener {

    private boolean created = false;
    private String path = "";
    private String projectName = "";
    private String imageDir = "";
    private Main comp;
    private int MAP_WIDTH = 120;
    private int MAP_HEIGHT = 120;
    private int TILE_WIDTH = 32;
    private int TILE_HEIGHT = 32;

    public Project(Main comp) {
        this.comp = comp;
        comp.FileNewProject.addActionListener(this);
        comp.FileOpenProject.addActionListener(this);
        comp.FileSaveProject.addActionListener(this);
        comp.FileCloseProject.addActionListener(this);
        comp.FileExit.addActionListener(this);

        comp.MenuViewGrid.addActionListener(this);
        comp.MenuViewTilePosition.addActionListener(this);

        comp.ScrollPaneWorkspaceRender.setVisible(false);
    }

    /**
     * Membuat project baru
     */
    public void newProject() {
        if (confirmCloseProject() != JOptionPane.CANCEL_OPTION || isCreated() == false) {
            ProjectDialog pd = new ProjectDialog(comp);
            pd.setModal(true);
            pd.setVisible(true);
        }
    }

    /**
     * Membuka project yang sudah dibuat sebelumnya
     */
    public void openProject() {
        if (isCreated() && confirmCloseProject() == 2) {
            return;
        }
        JFileChooser jf = new JFileChooser();
        jf.setDialogType(JFileChooser.OPEN_DIALOG);
        jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jf.setFileHidingEnabled(false);
        jf.setAcceptAllFileFilterUsed(false);
        MapFileFilter mff = new MapFileFilter("xml", "XML Only");
        jf.setFileFilter(mff);

        jf.showOpenDialog(comp);
        File file = jf.getSelectedFile();
        if (file != null) {
            comp.mapOperation.openMap(file.getAbsolutePath());
            comp.project.setCreated(true);
            comp.imPanelWidget.reloadAll();
            comp.ipPanelWidget.reloadAll();
            comp.osPanelWidget.reloadAll();
            comp.lmControlPanel.getTable().revalidate();            
            comp.lmControlPanel.gridWorkspace.drawGrid();
            comp.WorkspaceRender.setPreferredSize(new Dimension(MAP_WIDTH * TILE_WIDTH, MAP_HEIGHT * TILE_HEIGHT));
            comp.WorkspaceRender.revalidate();
            comp.ScrollPaneWorkspaceRender.setVisible(true);
            comp.repaint();
        }
    }

    /**
     * Konformasi Penutupan Project
     * @return
     */
    private int confirmCloseProject() {
        if (isCreated()) {
            int save = JOptionPane.showConfirmDialog(comp, "Project " + getProjectName() + " will be close!!!\nDo you want to save it?");
            if (save == JOptionPane.YES_OPTION) {
                saveProject();
            } else if (save == JOptionPane.CANCEL_OPTION) {
                return save;
            }
            close();
            return save;
        }
        return JOptionPane.CANCEL_OPTION;
    }

    /**
     * Menyimpan Project
     */
    public boolean saveProject() {
        // saving project
        return true;
    }

    /**
     * Menutup project
     */
    public void closeProject() {
        if (confirmCloseProject() == JOptionPane.YES_OPTION) {
            close();
        }
    }

    public void close() {
        setProjectName("");
        setPath("");
        setImageDir("");
        setCreated(false);
        comp.imContainer.clearAll();
        comp.ipContainer.clearAll();
        comp.osContainer.clearAll();
        comp.layerContainer.clearAll();
        comp.imPanelWidget.reloadAll();
        comp.ipPanelWidget.reloadAll();
        comp.osPanelWidget.reloadAll();
        comp.lmControlPanel.getTable().revalidate();
        comp.ScrollPaneWorkspaceRender.setVisible(false);
        comp.WorkspaceRender.setPreferredSize(new Dimension(0, 0));
        comp.WorkspaceRender.revalidate();
        comp.WorkspaceBottomSprite.removeAll();
        comp.WorkspaceBottomPattern.removeAll();
        comp.repaint();
    }

    /**
     * Set Path untuk lokasi pembuatan peta
     * @param path
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * Mengambil direktori yang digunakan sebagai tempat kerja
     * @return nama direktori tempat kerja
     */
    public String getPath() {
        return path;
    }

    /**
     * Memberi nama sebuah project
     * @param projectName
     */
    public void setProjectName(String projectName) {
        this.projectName = projectName;
        if (projectName.trim().equals("")) {
            comp.setTitle("Map Generator");
        } else {
            comp.setTitle(projectName + " - Map Generator");
        }
    }

    /**
     * Mengambil nama project
     * @return
     */
    public String getProjectName() {
        return projectName;
    }

    /**
     * Memberikan status apakah project telah dibuat atau belum.
     * @param created true apabila project telah dibuat
     */
    public void setCreated(boolean created) {
        this.created = created;
    }

    /**
     * Memeriksa project apakah telah di buat atau tidak.
     * @return true apabila project telah dibuat
     */
    public boolean isCreated() {
        return created;
    }

    public void setImageDir(String imageDir) {
        this.imageDir = imageDir;

        // Setting path for Pattern Image Location
        comp.ipImageManager.setPath(imageDir);
    }

    public String getImageDir() {
        return imageDir;
    }

    public int getMapWidth() {
        return MAP_WIDTH;
    }

    public int getMapHeight() {
        return MAP_HEIGHT;
    }

    public int getTileWidth() {
        return TILE_WIDTH;
    }

    public int getTileHeight() {
        return TILE_HEIGHT;
    }

    public void setMapWidth(int MAP_WIDTH) {
        this.MAP_WIDTH = MAP_WIDTH;
    }

    public void setMapHeight(int MAP_HEIGHT) {
        this.MAP_HEIGHT = MAP_HEIGHT;
    }

    public void setTileWidth(int TILE_WIDTH) {
        this.TILE_WIDTH = TILE_WIDTH;
    }

    public void setTileHeight(int TILE_HEIGHT) {
        this.TILE_HEIGHT = TILE_HEIGHT;
    }

    public void actionPerformed(ActionEvent e) {
        Object ob = e.getSource();
        if (ob == comp.FileNewProject) {
            comp.project.newProject();
        } else if (ob == comp.FileOpenProject) {
            openProject();
        } else if (ob == comp.FileSaveProject) {
            comp.mapOperation.saveMap(comp.project.getPath() + File.separator + "Configure-Project.xml");
        } else if (ob == comp.FileCloseProject) {
            closeProject();
        } else if (ob == comp.FileExit) {
            System.exit(-1);
        } else if (ob == comp.MenuViewGrid || ob == comp.MenuViewTilePosition) {
            comp.lmControlPanel.gridWorkspace.drawGrid();
        }
    }
}